package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.StringUtil;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.constant.RedisMessageConstant;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.QiNiuUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *
 * </p>
 *
 * @author: Eric
 * @since: 2020/11/26
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealMobileController {

    @Reference
    private SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;

    /**
     * 套餐列表展示
     */
    @GetMapping("/getSetmeal")
    public Result getSetmeal(){
        Jedis redisConnect = jedisPool.getResource();
        String key = RedisMessageConstant.SETMEAL_LIST;

        if (!StringUtil.isEmpty(key)){
            String jsonStr = redisConnect.get(key);
            if (!StringUtil.isEmpty(jsonStr)) {
                log.debug("key不为空,key对应的value:{}",jsonStr);
                JSONArray array = JSONArray.parseArray(jsonStr);
                List<Setmeal> mealList = JSONObject.parseArray(array.toJSONString(), Setmeal.class);
                log.info("将套餐集合数据从redis取完并转化,准备响应客户端");
                return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS,mealList);
            }
        }

        // 查询所有的套餐
        List<Setmeal> list = setmealService.findAll();
        // 页面要显示图片，拼接图片的完整路径
        list.forEach(s->{s.setImg(QiNiuUtils.DOMAIN+s.getImg());});

        log.debug("正在准备将dao中查询到的数据存储到redis中,{}",list);
        Object json = JSON.toJSON(list);

        redisConnect.setex(key, 30*60, String.valueOf(json));
        redisConnect.close();
        log.info("查寻套餐集合数据存储redis完毕");
        return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS,list);
    }

    /**
     * 查询套餐详情
     */
    @GetMapping("/findDetailById")
    public Result findDetailById(int id){
        //获取redis连接
        Jedis redisConnect = jedisPool.getResource();
        //定义key
        String key = RedisMessageConstant.SETMEAL_DETAIL+id;

        if (!StringUtil.isEmpty(key)){
            String jsonStr = redisConnect.get(key);
            if (!StringUtil.isEmpty(jsonStr)) {
                log.debug("套餐详情的key不为空,key对应的value:{}",jsonStr);
                JSONObject jsonObject = JSON.parseObject(jsonStr);
                Setmeal setmeal = JSONObject.toJavaObject(jsonObject, Setmeal.class);
                System.out.println("setmeal------>"+setmeal);
                return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS,setmeal);
            }
        }

        Setmeal setmeal = setmealService.findDetailById(id);
        // 页面要显示图片，拼接图片的完整路径
        setmeal.setImg(QiNiuUtils.DOMAIN+setmeal.getImg());

        Object json = JSONObject.toJSON(setmeal);
        System.out.println("json------->"+json);
        redisConnect.setex(key, 30*60, String.valueOf(json));
        redisConnect.close();

        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
    }

    /**
     * 通过id查询
     */
    @GetMapping("/findById")
    public Result findById(int id){
        Setmeal setmeal = setmealService.findById(id);
        // 页面要显示图片，拼接图片的完整路径
        setmeal.setImg(QiNiuUtils.DOMAIN+setmeal.getImg());
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
    }
}
